package gui;

import houseHold.House;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.xml.transform.TransformerConfigurationException;

import locations.LocationArchive;

import org.xml.sax.SAXException;

import person.PersonAchive;
import reader.ScenarioBuilder;
import simulation.SimThread;
import toXML.LoadFamilyXML;
import toXML.LoadHouseXML;
import toXML.SaveFamilyXML;
import toXML.SaveHouseXML;
import RoomPanel.Aproach;
import RoomPanel.PanelBuilder;
import RoomPanel.RoomPanel;
import action.ActionPool;
import constants.Constants;
import db.DBConnection;

public class MainGUI extends JFrame {

	private static JLabel FamilyStatus = null;
	private static JLabel HouseStatus = null;
	private static MainGUI instance = new MainGUI();
	private static JLabel ScenarioStatus = null;
	private static final long serialVersionUID = 1L;
	private static JLabel SimulationStatus = null;

	public static void init() {
		instance.initialize();
	}

	public static MainGUI getInstance() {
		return instance;
	}

	public static void setFamilyStatus(Color c, String msg) {
		FamilyStatus.setText(msg);
		FamilyStatus.setForeground(c);
		if (c.equals(Constants.green))
			Stage2Status = true;
		state();
	}

	public static void setHouseStatus(Color c, String msg) {
		HouseStatus.setText(msg);
		HouseStatus.setForeground(c);
		if (c.equals(Constants.green))
			Stage1Status = true;
		state();
	}

	public static void setScenarioStatus(Color c, String msg) {
		ScenarioStatus.setText(msg);
		ScenarioStatus.setForeground(c);
		if (c.equals(Constants.green))
			Stage3Status = true;
		state();
	}

	public static void setSimulationStatus(Color c, String msg) {
		SimulationStatus.setText(msg);
		SimulationStatus.setForeground(c);
	}

	private static JCheckBox CheckVisual = null;
	private JButton CreateFamily = null;
	private JButton EditFamily = null;
	private JButton EditHouse = null;
	private static JPanel FamilyPanel = null;
	private static JButton FamilyValidate = null;
	private JButton ScenarioMenu = null;
	private static JPanel HousePanel = null;
	private static JButton HouseValidate = null;
	private JPanel jContentPane = null;
	private JButton LoadFamily = null;
	private JButton LoadHouse = null;
	private JButton NewHouse = null;
	private static JButton RunSimulation = null;
	private JButton SaveFamily = null;
	private JButton SaveHouse = null;
	private static JButton ScenarioValidate = null;
	private static JPanel ScenarioPanel = null;

	private static JPanel SimulationPanel = null;

	private static JProgressBar SimulationProgress = null;

	private static JButton SImulationResults = null;

	private JLabel VisualSim = null;

	/**
	 * This is the default constructor
	 */
	public MainGUI() {
		super();
		initialize();
		changeAppState(1);
	}

	private static void state() {
		if (!testMode) {
			if (Stage1Status && Stage2Status)
				changeAppState(2);
			if (Stage3Status)
				changeAppState(3);
		}
	}

	/**
	 * This method initializes CheckVisual
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckVisual() {
		if (CheckVisual == null) {
			CheckVisual = new JCheckBox();
			CheckVisual.setOpaque(true);
			CheckVisual.setBounds(new Rectangle(125, 125, 25, 25));
			CheckVisual.setBackground(new Color(250, 250, 250));
		}
		return CheckVisual;
	}

	/**
	 * This method initializes CreateFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreateFamily() {
		if (CreateFamily == null) {
			CreateFamily = new JButton();
			CreateFamily.setBounds(new Rectangle(20, 90, 110, 25));
			CreateFamily.setForeground(Constants.green);
			CreateFamily.setText("New Family");
			CreateFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					PersonAchive.reset();
					FamilyDialog fd = new FamilyDialog(getInstance());
					getInstance().setEnabled(false);
					fd.setVisible(true);
				}
			});
		}
		return CreateFamily;
	}

	/**
	 * This method initializes EditFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEditFamily() {
		if (EditFamily == null) {
			EditFamily = new JButton();
			EditFamily.setBounds(new Rectangle(20, 160, 90, 25));
			EditFamily.setForeground(new Color(51, 51, 51));
			EditFamily.setText("Edit");
			EditFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FamilyDialog fd = new FamilyDialog(getInstance());
					getInstance().setEnabled(false);
					fd.setVisible(true);
				}
			});
		}
		return EditFamily;
	}

	private static void validate(JPanel p, boolean status) {
		Component[] com = p.getComponents();
		if (status == false) {
			p.setBackground(Constants.redInvalid);
		} else
			p.setBackground(new Color(250, 250, 250));
		for (int a = 0; a < com.length; a++) {
			com[a].setEnabled(status);
		}
	}

	/**
	 * This method initializes EditHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEditHouse() {
		if (EditHouse == null) {
			EditHouse = new JButton();
			EditHouse.setBounds(new Rectangle(20, 160, 90, 25));
			EditHouse.setForeground(new Color(51, 51, 51));
			EditHouse.setText("Edit");
			EditHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					prepare(10);
					HouseEditor.getInstance().setVisible(true);
					MainGUI.getInstance().setEnabled(false);
				}
			});
		}
		return EditHouse;
	}

	private static void initVals() {
		houseVal = false;
		familyVal = false;
		scenarioVal = false;
	}

	public static boolean houseVal, familyVal, scenarioVal, simVal;

	private static void changeAppState(int state) {
		if (testMode) {
			houseVal = true;
			familyVal = true;
			scenarioVal = true;
			Stage1Status = true;
			Stage2Status = true;
			Stage2Status = true;
			validate(FamilyPanel, true);
			validate(HousePanel, true);
			validate(ScenarioPanel, true);
			validate(SimulationPanel, true);
			setFamilyStatus(Constants.red, "Test Mode");
			setHouseStatus(Constants.red, "Test mode");
			setScenarioStatus(Constants.red, "Test mode");
			setSimulationStatus(Constants.red, "Test mode");
		} else {
			System.out.println("CurrentState = " + state);
			initVals();
			switch (state) {
			case 1:
				initVals();
				validate(FamilyPanel, true);
				validate(HousePanel, true);
				validate(ScenarioPanel, false);
				validate(SimulationPanel, false);
				houseVal = false;
				familyVal = false;
				setVal();
				break;
			case 2:
				validate(FamilyPanel, false);
				validate(HousePanel, false);
				validate(ScenarioPanel, true);
				validate(SimulationPanel, false);
				scenarioVal = false;
				setVal();
				break;
			case 3: {
				validate(FamilyPanel, false);
				validate(HousePanel, false);
				validate(ScenarioPanel, false);
				validate(SimulationPanel, true);
				SimSpeed.setEnabled(false);
				CheckVisual.setEnabled(false);
				PrepareSimulation.setEnabled(true);
				RunSimulation.setEnabled(false);
				StopSimulation.setEnabled(false);
				SImulationResults.setEnabled(false);
				break;
			}
			}
		}
	}

	private static boolean Stage1Status = false, Stage2Status = false,
			Stage3Status = false;

	public static void setVal() {
		System.out
				.println("setting cals " + houseVal + familyVal + scenarioVal);
		HouseValidate.setEnabled(houseVal);
		FamilyValidate.setEnabled(familyVal);
		ScenarioValidate.setEnabled(scenarioVal);

	}

	/**
	 * This method initializes FamilyPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getFamilyPanel() {
		if (FamilyPanel == null) {
			FamilyStatus = new JLabel();
			FamilyStatus.setBounds(new Rectangle(15, 30, 140, 30));
			FamilyStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setFamilyStatus(Constants.red, "Family Inactive");
			FamilyPanel = new JPanel();
			FamilyPanel.setLayout(null);
			FamilyPanel.setBounds(new Rectangle(210, 10, 170, 280));
			FamilyPanel.setBackground(new Color(250, 250, 250));
			FamilyPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Family",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));

			FamilyPanel.add(getCreateFamily(), null);
			FamilyPanel.add(getLoadFamily(), null);
			FamilyPanel.add(getEditFamily(), null);
			FamilyPanel.add(getSaveFamily(), null);
			FamilyPanel.add(FamilyStatus, null);
			FamilyPanel.add(getFamilyValidate(), null);
		}
		return FamilyPanel;
	}

	/**
	 * This method initializes FamilyValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getFamilyValidate() {
		if (FamilyValidate == null) {
			FamilyValidate = new JButton();
			FamilyValidate.setBounds(new Rectangle(20, 120, 110, 25));
			FamilyValidate.setForeground(new Color(0, 0, 102));
			FamilyValidate.setText("Validate");
			FamilyValidate.setEnabled(false);
			FamilyValidate
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (PersonAchive.getCount() > 0)
								setFamilyStatus(Constants.green,
										"Family Active");
							else
								setFamilyStatus(Constants.red,
										"Family Inactive");
						}
					});
		}
		return FamilyValidate;
	}

	/**
	 * This method initializes ScenarioMenu
	 * 
	 * @return javax.swing.JButton
	 */
	ScenarioDialog sd = null;
	private JButton EditScenario = null;
	private static JButton PrepareSimulation = null;

	private JButton getScenarioMenu() {
		if (ScenarioMenu == null) {
			ScenarioMenu = new JButton();
			ScenarioMenu.setBounds(new Rectangle(20, 90, 110, 25));
			ScenarioMenu.setForeground(Constants.green);
			ScenarioMenu.setEnabled(true);
			ScenarioMenu.setText("Build new");
			ScenarioMenu.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					LocationArchive.init();
					sd = new ScenarioDialog(MainGUI.getInstance());
					ScenarioValidate.setEnabled(false);
					scenarioVal = false;
					sd.setVisible(true);
				}
			});
		}
		return ScenarioMenu;
	}

	/**
	 * This method initializes HousePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getHousePanel() {
		if (HousePanel == null) {
			HouseStatus = new JLabel();
			HouseStatus.setBounds(new Rectangle(15, 30, 140, 30));
			HouseStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setHouseStatus(Constants.red, "House Inactive");
			HousePanel = new JPanel();
			HousePanel.setLayout(null);
			HousePanel.setBounds(new Rectangle(10, 10, 170, 280));
			HousePanel.setBackground(new Color(250, 250, 250));
			HousePanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "House",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));
			HousePanel.add(HouseStatus, null);
			HousePanel.add(getNewHouse(), null);
			HousePanel.add(getLoadHouse(), null);
			HousePanel.add(getEditHouse(), null);
			HousePanel.add(getSaveHouse(), null);

			HousePanel.add(getHouseValidate(), null);
			HousePanel.add(getNewHouse(), null);

		}
		return HousePanel;
	}

	/**
	 * This method initializes HouseValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getHouseValidate() {
		if (HouseValidate == null) {
			HouseValidate = new JButton();
			HouseValidate.setBounds(new Rectangle(20, 120, 110, 25));
			HouseValidate.setForeground(new Color(0, 0, 102));
			HouseValidate.setText("Validate");
			HouseValidate.setEnabled(false);
			HouseValidate
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							Aproach.computeCount(HouseEditor.panels);
							if (House.getInstance().isActive()) {
								try {
									SaveHouseXML.save(Constants.activeFile);
								} catch (FileNotFoundException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (TransformerConfigurationException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (SAXException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								setHouseStatus(Constants.green, "House Active");

							} else
								setHouseStatus(Constants.red, "House Inactive");
						}
					});
		}
		return HouseValidate;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.white);
			jContentPane.add(getHousePanel(), null);
			jContentPane.add(getFamilyPanel(), null);
			jContentPane.add(getScenarioPanel(), null);
			jContentPane.add(getSimulationPanel(), null);
			jContentPane.add(getResetFrom(), null);
			jContentPane.add(getExit(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes LoadFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadFamily() {
		if (LoadFamily == null) {
			LoadFamily = new JButton();
			LoadFamily.setBounds(new Rectangle(20, 190, 90, 25));
			LoadFamily.setForeground(new Color(51, 51, 51));
			LoadFamily.setText("Load");
			LoadFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						JFileChooser fc = new JFileChooser();
						int returnVal = fc.showOpenDialog(MainGUI.this);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							File file = fc.getSelectedFile();
							LoadFamilyXML.loadXML(file);
							setFamilyStatus(Constants.blue, "Family loaded");
						}
					} catch (Exception e1) {
						FamilyValidate.setEnabled(false);
						setFamilyStatus(Constants.red, "Load error");
					}
				}
			});
		}
		return LoadFamily;
	}

	/**
	 * This method initializes LoadHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadHouse() {
		if (LoadHouse == null) {
			LoadHouse = new JButton();
			LoadHouse.setBounds(new Rectangle(20, 190, 90, 25));
			LoadHouse.setForeground(new Color(51, 51, 51));
			LoadHouse.setText("Load");
			LoadHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						House.reset();
						HouseEditor.panels.clear();
						HouseEditor.getInstance().RoomBuilder.removeAll();
						JFileChooser fc = new JFileChooser();
						int returnVal = fc.showOpenDialog(MainGUI.this);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							File file = fc.getSelectedFile();
							LoadHouseXML.loadXML(file);
						}
						prepare(50);
						setHouseStatus(Constants.blue, "House Loaded");
					} catch (Exception e1) {
						e1.printStackTrace();
						HouseValidate.setEnabled(false);
						setHouseStatus(Constants.red, "Load error");
					}

				}
			});
		}
		return LoadHouse;
	}

	/**
	 * This method initializes NewHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getNewHouse() {
		if (NewHouse == null) {
			NewHouse = new JButton();
			NewHouse.setBounds(new Rectangle(20, 90, 110, 25));
			NewHouse.setForeground(Constants.green);
			NewHouse.setText("New House");
			NewHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					House.reset();
					HouseEditor.panels.clear();
					HouseEditor.getInstance().RoomBuilder.removeAll();
					HouseEditor.getInstance().setVisible(true);
					MainGUI.getInstance().setEnabled(false);
				}
			});
		}
		return NewHouse;
	}

	/**
	 * This method initializes RunSimulation
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRunSimulation() {
		if (RunSimulation == null) {
			RunSimulation = new JButton();
			RunSimulation.setBounds(new Rectangle(20, 150, 130, 25));
			RunSimulation.setForeground(Constants.green);
			RunSimulation.setEnabled(false);
			RunSimulation.setText("Run Simulation");
			RunSimulation
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							boolean visual = CheckVisual.isSelected();
							SimThread.visual = visual;
							simThread = new Thread(new SimThread());
							simThread.start();
							SimSpeed.setEnabled(false);
						}
					});
		}
		return RunSimulation;
	}

	private Thread simThread;

	/**
	 * This method initializes SaveFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveFamily() {
		if (SaveFamily == null) {
			SaveFamily = new JButton();
			SaveFamily.setBounds(new Rectangle(20, 220, 90, 25));
			SaveFamily.setForeground(new Color(51, 51, 51));
			SaveFamily.setText("Save");
			SaveFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						try {
							SaveFamilyXML.save(file);
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (TransformerConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SAXException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			});
		}
		return SaveFamily;
	}

	/**
	 * This method initializes SaveHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveHouse() {
		if (SaveHouse == null) {
			SaveHouse = new JButton();
			SaveHouse.setBounds(new Rectangle(20, 220, 90, 25));
			SaveHouse.setForeground(new Color(51, 51, 51));
			SaveHouse.setText("Save");
			SaveHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						try {
							SaveHouseXML.save(file);
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (TransformerConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SAXException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			});
		}
		return SaveHouse;
	}

	/**
	 * This method initializes ScenarioValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getScenarioValidate() {
		if (ScenarioValidate == null) {
			ScenarioValidate = new JButton();
			ScenarioValidate.setBounds(new Rectangle(20, 120, 110, 25));
			ScenarioValidate.setForeground(new Color(0, 0, 102));
			ScenarioValidate.setText("Validate");
			ScenarioValidate.setEnabled(false);
			ScenarioValidate
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							ActionPool.clear();
							for (int i = 0; i < ScenarioDialog.scenarios.size(); i++) {
								ScenarioBuilder sb = new ScenarioBuilder();
								sb.build(ScenarioDialog.scenarios.get(i));
							}
							setScenarioStatus(Constants.green,
									"Scenario Active");

						}
					});
		}
		return ScenarioValidate;
	}

	/**
	 * This method initializes ScenarioPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getScenarioPanel() {
		if (ScenarioPanel == null) {
			ScenarioStatus = new JLabel();
			ScenarioStatus.setBounds(new Rectangle(15, 30, 140, 30));
			ScenarioStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setScenarioStatus(Constants.red, "Scenario inactive");
			ScenarioPanel = new JPanel();
			ScenarioPanel.setLayout(null);
			ScenarioPanel.setBounds(new Rectangle(410, 10, 170, 280));
			ScenarioPanel.setBackground(new Color(250, 250, 250));
			ScenarioPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Scenario",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));

			ScenarioPanel.add(getScenarioMenu(), null);
			ScenarioPanel.add(ScenarioStatus, null);
			ScenarioPanel.add(getScenarioValidate(), null);
			ScenarioPanel.add(getEdit(), null);
		}
		return ScenarioPanel;
	}

	/**
	 * This method initializes SimulationPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getSimulationPanel() {
		if (SimulationPanel == null) {
			SimulationStatus = new JLabel();
			SimulationStatus.setBounds(new Rectangle(15, 30, 140, 30));
			SimulationStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setSimulationStatus(Constants.red, "Simulation Inactve");
			VisualSim = new JLabel();
			VisualSim.setBounds(new Rectangle(20, 125, 103, 25));
			VisualSim.setForeground(Constants.blue);
			VisualSim.setText("Visual Simulation:");
			SimulationPanel = new JPanel();
			SimulationPanel.setLayout(null);
			SimulationPanel.setBounds(new Rectangle(610, 10, 170, 280));
			SimulationPanel.setBackground(new Color(250, 250, 250));
			SimulationPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory
							.createTitledBorder(BorderFactory.createLineBorder(
									Color.gray, 5), "Simulation",
									TitledBorder.DEFAULT_JUSTIFICATION,
									TitledBorder.DEFAULT_POSITION, new Font(
											"SansSerif", Font.BOLD, 14),
									Constants.blue)));

			SimulationPanel.add(getRunSimulation(), null);
			SimulationPanel.add(getSImulationResults(), null);
			SimulationPanel.add(VisualSim, null);
			SimulationPanel.add(getCheckVisual(), null);
			SimulationPanel.add(SimulationStatus, null);
			SimulationPanel.add(getSimulationProgress(), null);
			SimulationPanel.add(getPrepareSimulation(), null);
			SimulationPanel.add(getStopSimulation(), null);
			SimulationPanel.add(getSimSpeed(), null);
		}
		return SimulationPanel;
	}

	/**
	 * This method initializes SimulationProgress
	 * 
	 * @return javax.swing.JProgressBar
	 */
	public static void setProgressBarValue(int value) {
		SimulationProgress.setValue(value);
		SimulationProgress.repaint();
	}

	private JProgressBar getSimulationProgress() {
		if (SimulationProgress == null) {
			SimulationProgress = new JProgressBar();
			SimulationProgress.setBounds(new Rectangle(20, 210, 130, 25));

		}
		return SimulationProgress;
	}

	/**
	 * This method initializes SImulationResults
	 * 
	 * @return javax.swing.JButton
	 */
	ResultDialog rd;
	private static JButton StopSimulation = null;
	public static JComboBox SimSpeed = null;
	private JButton ResetFrom = null;
	private JButton Exit = null;

	private JButton getSImulationResults() {
		if (SImulationResults == null) {
			SImulationResults = new JButton();
			SImulationResults.setBounds(new Rectangle(20, 240, 130, 25));
			SImulationResults.setForeground(new Color(0, 0, 102));
			SImulationResults.setEnabled(false);
			SImulationResults.setText("Get Results");
			SImulationResults
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String sid = Constants.id;
							rd = new ResultDialog(MainGUI.this, DBConnection
									.getSimData(sid));
							rd.setVisible(true);
						}
					});
		}
		return SImulationResults;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(810, 369);
		this.setContentPane(getJContentPane());
		this.setTitle("Home-Sim");
		changeAppState(1);
		// autoInit();
	}

	private static void prepare(int time) {
		HouseEditor.panels.clear();
		Aproach.computeCount(HouseEditor.panels);
		HouseEditor.getInstance().RoomBuilder.removeAll();
		for (int i = 0; i < House.getInstance().getRoomCount(); i++) {
			RoomPanel p = PanelBuilder.newPanel(House.getInstance().getRoom(i));
			p.addPopUpMenu();
			HouseEditor.panels.add(p);
			HouseEditor.getInstance().RoomBuilder.add(p);
			HouseEditor.getInstance().RoomBuilder.repaint();
			if (time > 0)
				try {
					HouseEditor.getInstance().setVisible(true);
					MainGUI.getInstance().setEnabled(false);
					Thread.sleep(time);
					MainGUI.getInstance().setEnabled(true);
					HouseEditor.getInstance().setVisible(false);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		}
		Aproach.computeCount(HouseEditor.panels);
	}

	/**
	 * This method initializes Clear
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEdit() {
		if (EditScenario == null) {
			EditScenario = new JButton();
			EditScenario.setBounds(new Rectangle(20, 160, 90, 25));
			EditScenario.setForeground(new Color(51, 51, 51));
			EditScenario.setText("Edit");
			EditScenario.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (sd == null) {
						LocationArchive.init();
						sd = new ScenarioDialog(MainGUI.getInstance());
					}
					sd.setVisible(true);
				}
			});
		}
		return EditScenario;

	}

	public static void ActivateSV(boolean value) {
		System.out.println(value);
		ScenarioValidate.setEnabled(value);
		ScenarioValidate.repaint();

	}

	/**
	 * This method initializes PrepareSimulation
	 * 
	 * @return javax.swing.JButton
	 */

	private static void prepareDB() {
		try {
			DBConnection.initConnection();
			DBConnection.CreateTables();
			DBConnection.insertMainData();
			RunSimulation.setEnabled(true);
			SimSpeed.setEnabled(true);
			CheckVisual.setEnabled(true);
			SImulationResults.setEnabled(true);
			StopSimulation.setEnabled(true);
		} catch (Exception e1) {
			setSimulationStatus(Constants.red, "DB inactive");
		}
	}

	private JButton getPrepareSimulation() {
		if (PrepareSimulation == null) {
			PrepareSimulation = new JButton();
			PrepareSimulation.setBounds(new Rectangle(20, 70, 130, 25));
			PrepareSimulation.setForeground(Constants.green);
			PrepareSimulation.setEnabled(true);
			PrepareSimulation.setText("Load Data");
			PrepareSimulation
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							setSimulationStatus(Constants.green,
									"Preparing simulation");
							Constants.id = House.getInstance()
									.getHouseIdentifier()
									+ "_" + PersonAchive.getPersonIdentifier();
							try {
								Thread.sleep(100);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							try {
								prepareDB();
								setSimulationStatus(Constants.green,
										"Data Loaded");
							} catch (Exception e1) {
								setSimulationStatus(Constants.red,
										"DB inactive");
							}
						}
					});
		}
		return PrepareSimulation;
	}

	private static void autoInit() {
		File fam = new File("warden.xml");
		File house = new File("house.xml");
		House.reset();
		PersonAchive.reset();
		LoadFamilyXML.loadXML(fam);
		LoadHouseXML.loadXML(house);
		prepare(300);
		File s1 = new File("Scenario/Alice.sce");
		File s2 = new File("Scenario/Bob.sce");
		File s3 = new File("Scenario/Eve.sce");
		File s4 = new File("Scenario/Dave.sce");
		File s5 = new File("Scenario/Weather.sce");
		LocationArchive.init();
		ActionPool.clear();
		Vector<String> data = new Vector<String>();
		data.add(read(s1));
		data.add(read(s2));
		data.add(read(s3));
		data.add(read(s4));
		data.add(read(s5));
		for (int i = 0; i < data.size(); i++) {
			ScenarioBuilder sb = new ScenarioBuilder();
			sb.build(data.get(i));
		}
		Constants.id = House.getInstance().getHouseIdentifier() + "_"
				+ PersonAchive.getPersonIdentifier();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			DBConnection.initConnection();
			DBConnection.CreateTables();
			DBConnection.insertMainData();
		} catch (Exception e) {
			System.err.println("DATABASE OFFLINE");
		}
	}

	public static String read(File f) {
		String result = new String(), nextLine = new String();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			while ((nextLine = reader.readLine()) != null)
				result = result.concat(nextLine).concat("\n");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * This method initializes StopSimulation
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getStopSimulation() {
		if (StopSimulation == null) {
			StopSimulation = new JButton();
			StopSimulation.setForeground(Constants.red);
			StopSimulation.setText("Stop Simulation");
			StopSimulation.setToolTipText("WARNING!");
			StopSimulation.setEnabled(false);
			StopSimulation.setBounds(new Rectangle(20, 180, 130, 25));
			StopSimulation
					.addActionListener(new java.awt.event.ActionListener() {
						@SuppressWarnings("deprecation")
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (simThread != null) {
								try {
									simThread.stop();
									simulation.SimRunner.StopSimulation();
									setProgressBarValue(0);
									SimSpeed.setEnabled(true);
									setSimulationStatus(Color.red,
											"Simulation Aborted");
								} catch (Exception e1) {
									setSimulationStatus(Color.red,
											"Simulation FAIL");
								}

							}
						}
					});
		}
		return StopSimulation;
	}

	/**
	 * This method initializes SimSpeed
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getSimSpeed() {
		if (SimSpeed == null) {
			SimSpeed = new JComboBox();
			SimSpeed.setBounds(new Rectangle(20, 100, 130, 25));
			SimSpeed.setBackground(new Color(250, 250, 250));
			SimSpeed.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					String select = (String) SimSpeed.getSelectedItem();
					if (select.equalsIgnoreCase("Slow"))
						Constants.time = 60;
					if (select.equalsIgnoreCase("Normal"))
						Constants.time = 120;
					if (select.equalsIgnoreCase("Fast"))
						Constants.time = 210;
				}
			});
			SimSpeed.addItem("Slow");
			SimSpeed.addItem("Normal");
			SimSpeed.addItem("Fast");
		}
		return SimSpeed;
	}

	/**
	 * This method initializes ResetFrom
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getResetFrom() {
		if (ResetFrom == null) {
			ResetFrom = new JButton();
			ResetFrom.setText("Reset Form");
			ResetFrom.setForeground(Constants.red);
			ResetFrom.setBounds(new Rectangle(257, 300, 120, 25));
			ResetFrom.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setFamilyStatus(Constants.red, "Family inactive");
					setScenarioStatus(Constants.red, "Scenario inactive");
					setHouseStatus(Constants.red, "House inactive");
					setSimulationStatus(Constants.red, "Simulation inactive");
					House.reset();
					PersonAchive.reset();
					changeAppState(1);
					Stage1Status = false;
					Stage2Status = false;
				}
			});
		}
		return ResetFrom;
	}

	/**
	 * This method initializes Exit
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getExit() {
		if (Exit == null) {
			Exit = new JButton();
			Exit.setText("Exit");
			Exit.setBounds(new Rectangle(410, 300, 120, 25));
			Exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(1);
				}
			});
		}
		return Exit;
	}

	public static boolean testMode = false;

	public static void loadTestData() {
		testMode = true;
		autoInit();
		prepareDB();
	}

} // @jve:decl-index=0:visual-constraint="10,10"
